﻿using ArticleandSiteData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SplitViewOptions.BusinessControl
{
    public sealed partial class WeiShequUserControl : UserControl
    {
        public WeiShequUserControl()
        {
            this.InitializeComponent();
            this.Loaded += WeiShequUserControl_Loaded;
        }

        private async void WeiShequUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            await Getdata();
        }
        private async Task Getdata()
        {
            PicInfoRootobject users = await BmobWebsiteData.GetLists<PicInfoRootobject>("https://api.bmob.cn/1/classes/sharepictable?order=-updatedAt");
            List<PicInfo> listresults = new List<PicInfo>();
            foreach (var a in users.results)
            {
                if (!string.IsNullOrEmpty(a.pictags))
                {
                    string str = a.pictags;
                    string[] sp = new string[] { "，", ",", ";", ":" };
                    string[] result = str.Split(sp, StringSplitOptions.RemoveEmptyEntries);
                    a.tags = result;
                    listresults.Add(a);
                }
                else
                {
                    listresults.Add(a);
                }
            }
            listbox.ItemsSource = listresults;
        }
        private void liketap(object sender, TappedRoutedEventArgs e)
        {
            SymbolIcon ob = sender as SymbolIcon;
            ob.Foreground = new SolidColorBrush(Colors.Red);
            ob.IsTapEnabled = false;
            PicInfo info = ob.DataContext as PicInfo;
            updateobj(info, "supportnumber", info.supportnumber);
            Getdata();
        }

        private void disliketap(object sender, TappedRoutedEventArgs e)
        {
            SymbolIcon ob = sender as SymbolIcon;
            ob.Foreground = new SolidColorBrush(Colors.Red);
            ob.IsTapEnabled = false;
            PicInfo info = ob.DataContext as PicInfo;
            updateobj(info, "lookdownnumber", info.lookdownnumber);
            Getdata();
        }
        private  async void updateobj(PicInfo o,string field,string value)
        {
            string number = "0";
            string id = o.objectid;
            string sr = "https://api.bmob.cn/1/classes/sharepictable/" + id;
           
            if(string.IsNullOrEmpty(value))
            {
                number = "1";
            }
            else
            {
                number=(int.Parse(value) +1).ToString();
            }
            Dictionary<string, string> json = new Dictionary<string, string>();
            json.Add(field, number);
            string jsonstrdic = RequestHelper.DictionaryTojsonstr(json);
            HttpResponseMessage r= await   BmobWebsiteData.Putjson<HttpResponseMessage>(sr, jsonstrdic);
            if(r.StatusCode==System.Net.HttpStatusCode.OK)
            {
                 App.showmessage("点赞成功");
            }
            else
            {
                App.showmessage("点赞失败");
            }
        }
        private void Border_Tapped(object sender, TappedRoutedEventArgs e)
        {
            try
            {
                Image tin = sender as Image;
                PicInfo info = tin.DataContext as PicInfo;                         
                updateobj(info, "supportnumber", info.supportnumber);
                if (string.IsNullOrEmpty(info.weburl)) return;

                App.Currenturl = new Uri(info.weburl, UriKind.RelativeOrAbsolute);
                App.Mainframe.Navigate(typeof(HomePage));
            }
            catch (Exception ex)
            {
            }
        }
    }
}
